from qgl.render import *

class MyCompiler(Compiler):
    def build_texture(self, node):
        key = (node.filename, node.mipmap, node.filter)
        if key in qgl_cache:
            node.texture = qgl_cache[key]
        else:
            if isinstance(node.filename, str):
                node.filename = pygame.image.load(node.filename)
            texture = GLTexture(node.filename, node.mipmap, node.filter, flip=False)
            qgl_cache[key] = texture
            node.texture = texture
        
class MyRender(Render):
    def visit_OrthoViewport(self, node):
        glViewport(*node.screen_dimensions)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        w,h = node.size
        glOrtho(0, w, h, 0, 0, 512)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

    def visit_Root(self, node):
        Render.visit_Root(self,node)
        glCullFace(GL_FRONT)
        glEnable(GL_CULL_FACE)

